package com.polaris.live.viewmodel.party.game

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.polaris.live.common.constant.CommonConst
import com.polaris.live.common.manager.withRetry
import com.polaris.live.common.mvvm.viewmodel.BaseViewModel
import com.polaris.live.common.network.AppException
import com.polaris.live.common.network.launchLoading
import com.polaris.live.common.network.request
import com.polaris.live.common.network.requestLoading
import com.polaris.live.common.network.state.ResultState
import com.polaris.live.common.util.ToastGravity
import com.polaris.live.constant.LiveConst
import com.polaris.live.im.sfs.PARTY_GAME_LIVESITEDOWN
import com.polaris.live.im.sfs.PARTY_GAME_LIVESITEUP
import com.polaris.live.im.sfs.bean.live.LiveGameSiteMessage
import com.polaris.live.im.sfs.core.SmartFoxQueueManager
import com.polaris.live.net.liveService
import com.polaris.live.resp.back_resp.LiveModifyPriceResp
import com.polaris.live.resp.back_resp.LiveStartGameResp
import com.polaris.live.resp.back_resp.PartyGameResp
import com.polaris.live.resp.back_resp.PartyGameSettlementResp
import com.polaris.live.resp.back_resp.PartyGameValidCodeResp
import com.polaris.live.resp.bean.ZegoGameEventBean
import kotlinx.coroutines.launch

/**
 * PartyAnchorModel  主播Model
 * @author Created by 天晴 on 2024/2/24 11:15
 * @since 1.0.0
 **/
class PartyGamePlayModel : BaseViewModel() {

    //上游戏位
    val liveGameSiteUpData = MutableLiveData(0)
    fun liveGameSiteUp(anchorId: Long) {
        launchLoading {
            try {
                SmartFoxQueueManager.sendLiveMessage(
                    PARTY_GAME_LIVESITEUP,
                    LiveGameSiteMessage(anchorId)
                )
            } catch (t: AppException) {
                if (t.errCode == CommonConst.ErrorCode.SENDGIFT || t.errCode == CommonConst.ErrorCode.GAME_BEANS) {
                    liveGameSiteUpData.value = liveGameSiteUpData.value?.plus(1)
                } else {
                    ToastGravity.showGravity(t.message)
                }
            }
        }
    }

    //下游戏位
    fun liveGameSiteDown(anchorId: Long) {
        launchLoading {
            try {
                SmartFoxQueueManager.sendLiveMessage(
                    PARTY_GAME_LIVESITEDOWN,
                    LiveGameSiteMessage(anchorId)
                )
            } catch (t: Throwable) {
                ToastGravity.showGravity(t.message)
            }
        }
    }


    /**
     * 开始游戏
     */
    val partyGameStartData = MutableLiveData<ResultState<Any>>()
    fun partyGameStart(liveId: Long, @LiveConst.BusinessType businessType: Int) {
        requestLoading(
            { liveService.partyGameStart(LiveStartGameResp(liveId, businessType)) },
            partyGameStartData
        )
    }


    /**
     * 获取单个数据
     */
    val getOnePartyGameData = MutableLiveData<ResultState<PartyGameResp>>()
    fun getOnePartyGame(gameId: Int) {
        requestLoading(
            { liveService.getOnePartyGame(gameId) },
            getOnePartyGameData
        )
    }

    /**
     * 获取getBaiShunGameCodeData
     */
    val getBaiShunGameExtData = MutableLiveData<ResultState<PartyGameValidCodeResp>>()
    fun getBaiShunGameCode(gameId: Int) {
        request(
            { liveService.getBaiShunGameCodeAndExt(gameId) },
            getBaiShunGameExtData
        )
    }

    /**
     * 修改游戏价格
     */
    val partyGamePriceData = MutableLiveData<ResultState<Any>>()
    fun partyGamePrice(liveId: Long, price: Long, assetType: Int) {
        requestLoading(
            { liveService.partyGamePrice(LiveModifyPriceResp(liveId, price, assetType)) },
            partyGamePriceData
        )
    }

    /**
     * 游戏结算
     */
    val partyGameResultData = MutableLiveData<ResultState<List<PartyGameSettlementResp>>>()
    fun partyGameResult(liveId: Long) {
        requestLoading(
            { liveService.partyGameResult(liveId) },
            partyGameResultData
        )
    }

    /**
     * 上传zego游戏事件
     */
    fun zegoGameEvent(zegoGameEventBean: ZegoGameEventBean) {
        viewModelScope.launch {
            try {
                //这里重试3次，并且第一次不等待
                withRetry(3, initDelay = 0L) {
                    liveService.zegoGameEvent(zegoGameEventBean)
                }
            } catch (_: Throwable) {
            }
        }
    }


    /**
     * 上传百顺游戏事件
     */
    fun baiShunGameEvent(zegoGameEventBean: ZegoGameEventBean) {
        viewModelScope.launch {
            try {
                //这里重试3次，并且第一次不等待
                withRetry(3, initDelay = 0L) {
                    liveService.baiShunGameEvent(zegoGameEventBean)
                }
            } catch (_: Throwable) {
            }
        }
    }
}